// pages/user/mymer/mymer.js
const app = getApp()
const utils = require('../../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pushList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMerList()
    // let _this = this
    // let random = Math.floor(Math.random() * 100)
    // if (random < 30) _this.getMerList()
    // else _this.error()
  },

  error: function () {
    wx.showLoading({
      title: '加载数据',
    })
    setTimeout(() => {
      wx.hideLoading()
      wx.showToast({
        title: '请求超时',
        duration: 3000
      })
    }, 5000)
  },

  /**
   * 获取发布的商保列表
   */
  getMerList: function() {
    let that = this,
        order = [],
        data
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/order-push/list?access_token=' + accessToken,
        success: res => {
          order = res.data.data.push_order
          if(res.data.status === 1) {
            for (let k in order) {
              order[k].service_at = utils.formatTimeThr(order[k].service_at, 'Y-M-D')
            }
            that.setData({
              pushList: order
            })
          }
        }
      })
    })
  },

  /**
   * 根据状态跳转页面
   */
  goPages: function(e) {
    let status = e.currentTarget.dataset.status,
        mid = e.currentTarget.dataset.mid,
        issigned = e.currentTarget.dataset.is_signed
    if(status == 0) {
      wx.navigateTo({
        url: '/pages/user/mymer/progress/progress?mid=' + mid,
      })
      
    } else if (status == 1 || status == 2) {
      wx.navigateTo({
        url: '/pages/user/mymer/sign/sign?mid=' + mid + '&issigned=' + issigned,
      })
    } else {
      wx.navigateTo({
        url: ''
      })
    }
  }
})